package org.bank.order.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

import org.bank.order.domain.Order;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;

public class OrderServiceImpl implements OrderService {
	
	private static final Logger log = LoggerFactory.getLogger("def");
	
	private SimpleJdbcTemplate jdbcTemplate;

	public List<Order> getOrders() {
		log.debug("getOrders() was called..");
		
		String sql = "SELECT * FROM sorder ORDER BY id";
		return jdbcTemplate.query(sql, new OrderRowMapper());
	}

	private class OrderRowMapper implements ParameterizedRowMapper<Order> {

		public Order mapRow(ResultSet rs, int rowNum) throws SQLException {
			Order order = new Order();
			order.setId(rs.getLong("id"));
			order.setName(rs.getString("name"));
			return order;
		}
	}
	
	public void testSvn() {
		;
	}

	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new SimpleJdbcTemplate(dataSource);
	}

}
